package com.hyh.music.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyh.music.Expections.AccountNotFoundException;
import com.hyh.music.Expections.PasswordErrorException;
import com.hyh.music.Expections.PasswordNullException;
import com.hyh.music.Expections.PhoneNullException;
import com.hyh.music.Result.Results;
import com.hyh.music.constant.MessageConstant;
import com.hyh.music.domain.domainDTO.ConsumerDTO;
import com.hyh.music.domain.domainDTO.ConsumerLoginDTO;
import com.hyh.music.domain.domainDTO.ConsumerPageDTO;
import com.hyh.music.domain.domains.Consumer;
import com.hyh.music.mapper.ConsumerMapper;
import com.hyh.music.service.ConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
@Slf4j
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper,Consumer> implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    /**
     * 分页查询
     *
     * @param consumerPageDTO
     * @return
     */
    @Override
    public Results Page(ConsumerPageDTO consumerPageDTO) {
        List<Consumer> consumers = consumerMapper.page(consumerPageDTO);

        Results results = new Results(consumers);
        return results;
    }

    /**
     * 添加新用户
     * @param consumerDTO
     */
    @Override
    public void addConsumer(ConsumerDTO consumerDTO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerDTO,consumer);
        consumer.setPassword(DigestUtils.md5DigestAsHex(consumer.getPassword().getBytes()));
        consumer.setCreateTime(LocalDateTime.now());
        consumer.setUpdateTime(LocalDateTime.now());
        consumerMapper.add(consumer);
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void delete(Integer id) {
        consumerMapper.delete(id);
    }

    /**
     * 用户登录
     * @param consumerLoginDTO
     * @return
     */
    @Override
    public Consumer login(ConsumerLoginDTO consumerLoginDTO) {
        String phone = consumerLoginDTO.getPhone();
        String password = consumerLoginDTO.getPassword();
        //0.用户传来的信息可能为空，要进行非空判断
        if (StrUtil.isBlank(phone)){
            throw new PhoneNullException(MessageConstant.PHONE_NULL);
        }
        if (StrUtil.isBlank(password)){
            throw new PasswordNullException(MessageConstant.PASSWORD_NULL);
        }
        //1.根据phone来查询用户，没有直接仍出用户不存在
        Consumer consumer = lambdaQuery()
                .eq(Consumer::getPhoneNum, phone)
                .one();
        if (consumer == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //2.查询出来后进行密码比对有错误直接报错
        if (!(DigestUtils.md5DigestAsHex(password.getBytes())).equals(consumer.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //3.都正确返回用户信息
        return consumer;
    }


}
